package GUI;


import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ClientConsole;

import Library.Search;

/**
 * @author mishel hen
 *
 */
/**
 * This class for order Book
 */
public class GUI_OrderBook extends JPanel
{
	/**
	 * Text field for name of book
	 */
	private JTextField bookIdText;
	/**
	 * client Connected
	 */
	private ClientConsole cc;
	/**
	 * panel for order Book
	 */
	private GUI_OrderBook ob = this;
	/**
	 * panel for back
	 */
	private JPanel back;
	
	/**
	 * @param clc 
	 * @param sob  
	 */
	public GUI_OrderBook(ClientConsole clc, final GUI_ShowOrderBook sob)
	{
		super();	
		cc = clc;
		initialy();
		back = sob;
	}
	
	/**
	 * @param clc 
	 * @param mms  
	 */
	public GUI_OrderBook(ClientConsole clc, final GUI_MainMenuStudent mms)
	{
		super();	
		cc = clc;
		initialy();
		back = mms;
	}
	
	
	public void initialy()
	{
		this.setSize(600, 400);
		
		JLabel lblTitle = new JLabel("Order Book");
		lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 24));
		lblTitle.setBounds(230, 20, 200, 40);
		add(lblTitle);
		
		JLabel lblTitle1 = new JLabel("Enter book's name :");
		lblTitle1.setBounds(60, 60, 400, 40);
		add(lblTitle1);
		
		bookIdText = new JTextField();
		bookIdText.setBounds(60, 100, 120, 30);
		add(bookIdText);
		bookIdText.setColumns(10);
		
		
		JButton btnorder = new JButton("order");
		btnorder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String text=bookIdText.getText();
				String s[]={"search",text,"0"};
				try {
					cc.client.sendToServer(s);
					while(cc.client.flag==0)
					    System.out.print("");
					    cc.client.flag=0;
					    Search search =(Search)cc.client.message;
					    if (search.booksList.isEmpty())
					    	JOptionPane.showMessageDialog(new JFrame(),"No books were find");
					    else
						    {
						    	GUI_SelectBook sb = new GUI_SelectBook(cc,search.booksList,ob);
								cc.cgui.setContentPane(sb);
						    }
					    
					} catch (IOException e) {
					e.printStackTrace();
				}	
			/*	String s[]={"Order book",text};
				try {
					cc.client.sendToServer(s);
					while(cc.client.flag==0)
					    System.out.print("");
					    cc.client.flag=0;
					    Books books =(Books)cc.client.message;
					    if(books.equals(null))
							JOptionPane.showMessageDialog(new JFrame(),"No books were find");
					    else
						{
							if(books.getNumCopies()<=0)
							{
								JOptionPane.showMessageDialog(new JFrame(),"No copies");
							}
							else
							{
								
							}
							
						}
					    
					    
		
					    	
				} catch (IOException e) {
					e.printStackTrace();
				}	// send message to server

				*/
		}});
		
		JButton btnBack = new JButton("Back");
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cc.cgui.setContentPane(back);
			}
		})
		;
		btnBack.setBounds(400, 320, 120, 30);
		add(btnBack);
	setLayout(null);
	
	btnorder.setBounds(60, 320, 120, 30);
	this.add(btnorder);
	this.setVisible(true);	
	

	}
	

}